Module 4: Serverless Deployment Frameworks

Developing Serverless Solutions on AWS - Live Session Guide

Module Overview

This module covers Infrastructure as Code (IaC) frameworks for building and deploying serverless applications on AWS.

Topics

1. Infrastructure as Code - Two Paradigms

Declarative (What)

  • You define the desired end state
  • The engine figures out how to get there
  • CloudFormation templates (YAML/JSON)
  • AWS SAM templates
  • Terraform HCL
Resources:
  MyBucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: my-app-data

Imperative (How)

  • You write step-by-step instructions
  • Full programming language power (loops, conditions)
  • AWS CDK (TypeScript, Python, Java, C#, Go)
  • Pulumi
  • SDK scripts
bucket = s3.Bucket(self, "MyBucket",
    bucket_name="my-app-data",
    removal_policy=RemovalPolicy.DESTROY
)

2. AWS CloudFormation

The foundation - all other frameworks compile down to CloudFormation templates.

Workflow

CODE YAML/JSON COMMIT S3 bucket CREATE Stack/StackSet DEPLOY Provision resources

Key Concepts

ConceptDescription
TemplateYAML/JSON file describing your infrastructure
StackA running instance of a template (collection of resources)
StackSetDeploy stacks across multiple accounts and regions
Change SetPreview changes before applying them
Drift DetectionDetect manual changes to stack resources

3. AWS Cloud Development Kit (CDK)

Define cloud infrastructure using familiar programming languages. CDK synthesizes to CloudFormation.

CDK Workflow

cdk init app --language python    # Initialize project
cdk synth                         # Generate CloudFormation template
cdk diff                          # Preview changes
cdk deploy                        # Deploy to AWS
cdk destroy                       # Tear down stack

CDK Construct Levels

LevelNameDescriptionExample
L1CFN ResourcesDirect CloudFormation mapping (1:1)CfnBucket
L2Curated ConstructsAWS-vetted with sensible defaults + helper methodss3.Bucket()
L3PatternsMulti-resource architectures in one constructLambdaRestApi()

Example: Lambda + API Gateway in CDK (Python)

from aws_cdk import Stack, aws_lambda as _lambda, aws_apigateway as apigw
from constructs import Construct

class MyServerlessStack(Stack):
    def __init__(self, scope: Construct, id: str, **kwargs):
        super().__init__(scope, id, **kwargs)

        # Lambda function
        handler = _lambda.Function(self, "Handler",
            runtime=_lambda.Runtime.PYTHON_3_12,
            code=_lambda.Code.from_asset("lambda"),
            handler="index.handler",
        )

        # API Gateway (L3 pattern - creates REST API + integrations)
        apigw.LambdaRestApi(self, "Endpoint", handler=handler)

4. AWS Serverless Application Model (SAM)

An extension of CloudFormation specifically designed for serverless. Adds shorthand resource types that expand into full CloudFormation on deploy.

SAM-Specific Resource Types

SAM TypeExpands To
AWS::Serverless::FunctionLambda + Role + Permissions + Event Sources
AWS::Serverless::ApiAPI Gateway REST API + Stage + Deployment
AWS::Serverless::HttpApiAPI Gateway HTTP API
AWS::Serverless::SimpleTableDynamoDB table with single-attribute primary key
AWS::Serverless::StateMachineStep Functions state machine
AWS::Serverless::LayerVersionLambda layer

SAM Template Example

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31    # This line makes it SAM!

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: app.handler
      Runtime: python3.12
      MemorySize: 256
      Timeout: 30
      Events:
        GetItems:
          Type: Api
          Properties:
            Path: /items
            Method: get
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref ItemsTable

  ItemsTable:
    Type: AWS::Serverless::SimpleTable

SAM CLI Commands

sam init                    # Create new SAM project (guided)
sam build                   # Build artifacts (dependencies)
sam local invoke            # Test Lambda locally (Docker)
sam local start-api         # Run API Gateway locally
sam local generate-event    # Generate sample events (S3, SQS, etc.)
sam validate                # Validate template
sam deploy --guided         # Deploy to AWS (interactive)
sam logs -n MyFunction      # Tail CloudWatch Logs
sam sync --watch            # Hot-reload changes (dev mode)

5. AWS Amplify

Full-stack development platform for web and mobile apps. Combines frontend hosting with backend cloud services.

Amplify Gen 2 (Current - 2024+)

Frontend

  • Git-based CI/CD hosting
  • SSR support (Next.js, Nuxt)
  • Amplify UI component libraries
  • React, Vue, Angular, Flutter

Backend (Gen 2)

  • TypeScript-first backend definition
  • Auth (Cognito), Data (AppSync/DynamoDB)
  • Storage (S3), Functions (Lambda)
  • Per-developer cloud sandbox
  • Deploys via CDK under the hood

Amplify Gen 2 Example

// amplify/data/resource.ts
import { defineData } from '@aws-amplify/backend';
import { a } from '@aws-amplify/data-schema';

const schema = a.schema({
  Todo: a.model({
    content: a.string(),
    isDone: a.boolean(),
  }).authorization(allow => [allow.owner()]),
});

export const data = defineData({ schema });

6. Framework Comparison

Feature CloudFormation CDK SAM Amplify
Language YAML/JSON TypeScript, Python, Java, C#, Go YAML/JSON TypeScript
Paradigm Declarative Imperative Declarative Imperative
Scope All AWS resources All AWS resources Serverless-focused Full-stack web/mobile
Local testing No No (use SAM with CDK) Yes (sam local) Yes (sandbox)
Deploys via Direct CloudFormation CloudFormation CDK/CloudFormation
Best for Enterprise IaC, compliance Complex infra, reusable patterns Lambda/API development Frontend + backend rapid dev

Key insight: All roads lead to CloudFormation. CDK, SAM, and Amplify all generate CloudFormation templates for deployment.

7. Live Demo: SAM Build & Deploy

# Step 1: Create a new SAM project
sam init --runtime python3.12 --name my-serverless-api --app-template hello-world

# Step 2: Explore the generated files
cd my-serverless-api
cat template.yaml         # SAM template
cat hello_world/app.py    # Lambda code

# Step 3: Test locally
sam build
sam local invoke HelloWorldFunction
sam local start-api       # Start local API on port 3000
# In another terminal: curl http://localhost:3000/hello

# Step 4: Deploy to AWS
sam deploy --guided
# Follow prompts: stack name, region, confirm changes

# Step 5: Test deployed API
curl https://<API_ID>.execute-api.us-west-2.amazonaws.com/Prod/hello/

# Step 6: View logs
sam logs -n HelloWorldFunction --tail

# Step 7: Cleanup
sam delete --stack-name my-serverless-api

8. What's New (2024-2025 Updates)

9. Module Summary

Key Takeaways

  • Declarative = define what you want (CloudFormation, SAM)
  • Imperative = define how to do it (CDK, Amplify Gen 2)
  • All frameworks deploy via CloudFormation
  • SAM = best for serverless-specific development + local testing
  • CDK = best for complex infrastructure with reusable patterns
  • Amplify = best for full-stack web/mobile rapid development
  • You'll likely use multiple frameworks across your apps

Decision Guide

  • Building Lambda + API Gateway? SAM
  • Complex multi-service infra? CDK
  • Full-stack with frontend? Amplify
  • Enterprise compliance/audit? CloudFormation
  • Need local testing? SAM CLI
  • Team uses TypeScript/Python? CDK

Developing Serverless Solutions on AWS - Module 4 | Live Session Guide

Last updated: June 2026